package demo;


import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author Klys
 * @Date 2023/3/9 14:09
 * @Description
 * @Version 1.0
 */
public class MySpring {

    private static Map<String, Person> personMap = new HashMap<>();

    static {
        Class clase = Person.class;//拿到类
        try {
            Person p = (Person) clase.newInstance();//反射
            Field[] fields = clase.getDeclaredFields();//得到全部属性
            for (Field field : fields) {    //遍历
                MyBean an = field.getAnnotation(MyBean.class);//拿到注解
                String value = an.value();
                field.setAccessible(true);
                field.set(p, value);
            }
            personMap.put("person1", p);
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    public static Person getBean(String id) {
        return personMap.get(id);
    }
}
